let db = require("../db/index");
const bcrypt = require("bcryptjs");

// 获取用户信息
exports.userData = (req, res) => {
    let account = req.query.account;
    let type = req.query.type;
    let outside;
    if (type == "b") {
        outside = "backstage_user";
    } else {
        outside = "user";
    }
    console.log(outside);
    let sql = `select * from \`${outside}\` where \`account\` = ? `;
    db.query(sql, account, (err, data) => {
        if (err) {
            return res.send("获取失败");
        }
        console.log(data);
        if (type != "b") {
            data[0].likes = JSON.parse(data[0].likes);
            data[0].collect = JSON.parse(data[0].collect);
            data[0].essay = JSON.parse(data[0].essay);
        }
        res.send(data);
    });
};
// 获取用户信息(id)
exports.userIdData = (req, res) => {
    let account = req.query.id;
    let type = req.query.type;
    let outside;
    if (type == "b") {
        outside = "backstage_user";
    } else {
        outside = "user";
    }
    let sql = `select * from \`${outside}\` where \`id\` = ? `;
    db.query(sql, account, (err, data) => {
        if (err) {
            return res.send("获取失败");
        }
        console.log(12121, data);
        res.send(data);
    });
};
// 修改密码
exports.setWord = (req, res) => {
    let data = req.body;
    let oldWord = data.oldWord;
    let newWord = data.newWord;
    let account = data.account;
    let type = data.type;
    let outside;
    if (type == "b") {
        outside = "backstage_user";
    } else {
        outside = "user";
    }
    let sql1 = `select password from \`${outside}\` where \`account\` = ? `;
    db.query(sql1, account, (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "修改错误",
            });
        }
        let password = data[0].password;
        let status = bcrypt.compareSync(oldWord, password);
        if (!status) {
            return res.send({
                s: 400,
                m: "修改失败,原密码不正确",
            });
        } else {
            let reg2 = /^[a-zA-Z]\w{5,17}$/;
            if (!reg2.test(newWord)) {
                return res.send({
                    s: 400,
                    m: "修改失败,密码必须以字母开头，长度在6~18之间，只能包含字母、数字和下划线",
                });
            } else {
                if (newWord == oldWord) {
                    return res.send({
                        s: 400,
                        m: "修改失败,新密码与旧密码一致",
                    });
                } else {
                    let sql2 = `UPDATE \`${outside}\` SET \`password\`= ? WHERE \`account\` = ?`;
                    newWord = bcrypt.hashSync(newWord, 10);
                    db.query(sql2, [newWord, account], (err, data) => {
                        if (err) {
                            return res.send({
                                s: 400,
                                m: "修改失败",
                            });
                        }
                        return res.send({
                            s: 200,
                            m: "修改成功",
                        });
                    });
                }
            }
        }
    });
};

// 修改头像
exports.setPic = (req, res) => {
    let pic = req.body.pic;
    let account = req.body.account;
    let type = req.body.type;
    if (type == "b") {
        outside = "backstage_user";
    } else {
        outside = "user";
    }
    let sql = `UPDATE \`${outside}\` SET \`user_pic\`= ? WHERE \`account\` = ?`;
    db.query(sql, [pic, account], (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "修改失败" + err,
            });
        }
        return res.send({
            s: 200,
            m: "修改成功",
        });
    });
};
